package com.example.login.Controller;

import com.example.login.Mapper.GoodsMap;
import com.example.login.Pojo.PCGoods;
import com.example.login.Pojo.mobileGoods;
import com.example.login.Pojo.mp3Goods;
import com.example.login.Service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
//修改信息
@Controller
public class changeItemController {
    //确认修改按钮
    @Autowired
    GoodsMap goodsMap;
    @Autowired
    ItemService UI;
    @Value("${ImagePath}")
    String filePath;
    @PostMapping("/merchant/mp3Change/{id}")
    public String change(@ModelAttribute("mp3") mp3Goods mp3, @PathVariable("id") int id,
                         @RequestParam("file") MultipartFile file)
    {
        String sku_code=mp3.getColor()+mp3.getROM();
        UI.UpdateInformation(id,sku_code,mp3.getPrice(),
                mp3.getNum(),mp3.getFright(),mp3.getCombination(),mp3.getIntroduction(),file.getOriginalFilename());
        //添加图片进入文件夹
        String fileDirPath = "src/main/resources/" + filePath + "//" + id+mp3.getColor()+mp3.getROM();
        store(fileDirPath,file);
        return "redirect:/merchantLook";
    }
    @PostMapping ("/merchant/pcChange/{id}")
    public String change(@ModelAttribute("pc") PCGoods pc, @PathVariable("id") int id,
                         @RequestParam("file") MultipartFile file)
    {
        String sku_code=pc.getColor()+pc.getROM()+pc.getRAM();
        UI.UpdateInformation(id,sku_code,pc.getPrice(),
                pc.getNum(),pc.getFright(),pc.getCombination(),pc.getIntroduction(),file.getOriginalFilename());
        //添加图片进入文件夹
        String fileDirPath = "src/main/resources/" + filePath + "//" + id+pc.getColor()+pc.getROM()+pc.getRAM();
        store(fileDirPath,file);
        return "redirect:/merchantLook";
    }
    @PostMapping("/merchant/mobileChange/{id}")
    public String change(@ModelAttribute("mobile") mobileGoods mobile, @PathVariable("id") int id,
                         @RequestParam("file") MultipartFile file)
    {
        String sku_code=mobile.getColor()+mobile.getROM()+mobile.getRAM()+mobile.getNetwork();
        UI.UpdateInformation(id,sku_code,mobile.getPrice(),
                mobile.getNum(),mobile.getFright(),mobile.getCombination(),mobile.getIntroduction(),file.getOriginalFilename());
        //添加图片进入文件夹
        String fileDirPath = "src/main/resources/" + filePath + "//" + id+mobile.getColor()+mobile.getROM()+
                mobile.getRAM()+mobile.getNetwork();
        store(fileDirPath,file);
        return "redirect:/merchantLook";
    }
    public void store(String fileDirPath,MultipartFile file)
    {
        File fileDir = new File(fileDirPath);

        if(!fileDir.exists()){
            // 递归生成文件夹
            fileDir.mkdirs();
        }
        try {
            File image= new File(fileDir.getAbsolutePath() + File.separator + file.getOriginalFilename());
            System.out.println(file.getOriginalFilename());
            file.transferTo(image);
            System.out.println(image.getAbsolutePath());//绝对路径
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改信息按钮
    @Autowired
    ItemService itemService;
    @RequestMapping("/merchant/change/{id}/{sku_code}")
    public String change(Model model, @PathVariable("id")int id,
                         @PathVariable("sku_code")String sku_code, RedirectAttributes attributes)
    {
        Object good=itemService.getItem(id,sku_code);
        if (good instanceof mp3Goods)
        {
            mp3Goods mp3good = (mp3Goods)good;
            System.out.println("why????");
            attributes.addFlashAttribute("mp3good",mp3good);
            return "redirect:/mp3";
        }
        else if (good instanceof PCGoods)
        {
            PCGoods pcGood=(PCGoods)good;
            attributes.addFlashAttribute("pc",pcGood);
            return "redirect:/pc";
        }
        else if(good instanceof mobileGoods)
        {
            mobileGoods mobileGood =(mobileGoods)good;
            attributes.addFlashAttribute("mobile",mobileGood);
            return "redirect:/mobile";
        }
        else
        {
            System.out.println("wrong");
            System.out.println(good);
            return "/";
        }

    }
}

